package com.hyg.lib_music.presenter;

import android.content.Context;

import com.hyg.lib_common.DataModel.Music.MusicCateData;
import com.hyg.lib_common.DataModel.Music.response.CateListResponse;
import com.hyg.lib_music.network.NetManager;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.Single;

public class MusicCateListPresenter implements MusicCateListContract.Presenter{

    private MusicCateListContract.View mFragment;
    private CloseableRxServiceExecutor executor;

    @Inject
    public MusicCateListPresenter(Context context,
                                  CloseableRxServiceExecutor executor) {
        this.executor = executor;
    }

    @Override
    public void setContractView(MusicCateListContract.View fragment) {
        mFragment = fragment;
    }

    @Override
    public void onStart() {

    }

    @Override
    public void onDestroy() {
    }

    /**
     * 分类列表
     *
     * @return
     */
    public Single<CateListResponse> cateListRequest(int cateId) {

        return Single.create(emitter -> emitter.onSuccess(NetManager.sharedManager().cateListRequest(cateId)));

    }

    @Override
    public void getCateList(int cateId){

        executor.execute(

                this.cateListRequest(cateId),
                response -> {
                    if (response.code == 200)
                    {
                        List<MusicCateData> data= response.data;
                        mFragment.setCateListData(data);
                    }else {
                        //失败逻辑
                        mFragment.setCateListData(null);
                    }
                },
                error -> {
                    //失败逻辑
                    mFragment.setCateListData(null);
//                    Logger.d(error);
                });
    }

    @Override
    public void existSubCateList(int cateId) {

        executor.execute(

                this.cateListRequest(cateId),
                response -> {
                    if (response.code == 200)
                    {
                        List<MusicCateData> data= response.data;
                        mFragment.enterSubCate(data);
                    }else {
                        //失败逻辑
                        mFragment.enterSubCate(null);
                    }
                },
                error -> {
//                    Logger.d(error);
                    mFragment.setCateListData(null);
                });
    }

}